//
//  CWQ_settingVC.m
//  BaxTer
//
//  Created by 陈武琦 on 16/9/6.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "CWQ_settingVC.h"
#import "CWQ_settingCell.h"
#import "CWQ_titleContentRIconCell.h"
#import "CWQ_personInfoVC.h"
#import "HELForgotPwdVC.h"
#import "BTAdviceVC.h"
#import "CWQ_fileClear.h"
#import "CWQ_aboutUsVC.h"
#import "BTModifyPwdVC.h"
#import "BTLoginVC.h"
#import "HELPDFfileCacheTool.h"
#import "JCAlertView.h"
#import "Masonry.h"

static NSString * const cellId = @"CWQ_settingCellID";
static NSString * const cellId2 = @"CWQ_titleContentRIconCellID";
@interface CWQ_settingVC ()

@end

@implementation CWQ_settingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self.tableView registerNib:[UINib nibWithNibName:@"CWQ_settingCell" bundle:nil] forCellReuseIdentifier:cellId];
        [self.tableView registerNib:[UINib nibWithNibName:@"CWQ_titleContentRIconCell" bundle:nil] forCellReuseIdentifier:cellId2];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.top.equalTo(self.view).with.offset(0);
        make.bottom.equalTo(self.view).with.offset(0);
    }];
    self.tableView.backgroundColor = HexRGB(0XECEBF0);
    [self bottomeBtnWith:@"退出登录"];
   
}


- (void)bottomeBtnWith:(NSString *)title{
    
    UIButton *footerBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    footerBtn.frame =CGRectMake(15, kScreenHeight -64-64, kScreenWidth - 30, 40);
    [footerBtn setTitle:title forState:UIControlStateNormal];
    [footerBtn setBackgroundColor:kMainColor];
    footerBtn.layer.cornerRadius = 5;
    footerBtn.layer.masksToBounds= YES;
    footerBtn.tag = 900;
    [footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerBtn addTarget:self action:@selector(logoutClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:footerBtn];
}

#pragma -mark-----退出登录
- (void)logoutClick {
    
    weakSelf_define;
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否确定退出当前账号" ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"否" Click:nil ButtonType:JCAlertViewButtonTypeWarn ButtonTitle:@"确认" Click:^{
        [HELAppManager loginOut];
        BTLoginVC *loginVC = [BTLoginVC new];
        BTNavigationContrller *nav = [[BTNavigationContrller alloc] initWithRootViewController:loginVC];
        [weakSelf presentViewController:nav animated:NO completion:nil];
    }];
    
   
}

#pragma mark ----------------tableview代理--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        return 10;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.row == 0 && indexPath.section == 1) {
        CWQ_titleContentRIconCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId2 forIndexPath:indexPath];
        //设置cell 的分割线
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        
        cell.title.text = @"清除缓存";
        WSJLog(@"%@",[NSString stringWithFormat:@"%0.1fM",[CWQ_fileClear fileSize]]);
        cell.content.text = @"计算中..";
        [HELPDFfileCacheTool sizeForAllCourseWithCallback:^(CGFloat size) {
             cell.content.text = [NSString stringWithFormat:@"%0.1fM",size];
        }];
        return cell;
    }
    
    
    
    CWQ_settingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    //设置cell 的分割线
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    if (indexPath.section == 0) {
        
        switch (indexPath.row) {
            case 0:
                cell.title.text = @"个人信息";
                break;
            case 1:
                cell.title.text = @"修改密码";
                break;
            default:
                break;
        }
        return cell;
    }
    
    
    
    switch (indexPath.row) {
        case 1:
            cell.title.text = @"意见反馈";
            break;
        case 2:
            cell.title.text = @"关于我们";
            break;
            
        default:
            break;
    }

    
    
       //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CWQ_personInfoVC *personInfoVC = [CWQ_personInfoVC new];
            [self.navigationController pushViewController:personInfoVC animated:YES];
        }else {
            BTModifyPwdVC *modifyPwdVC = [BTModifyPwdVC new];
            [self.navigationController pushViewController:modifyPwdVC animated:YES];
        }
        return;
    }
    

    
    switch (indexPath.row) {
        case 0:{
           
            [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否删除所有课程缓存" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"否" Click:nil ButtonType:JCAlertViewButtonTypeWarn ButtonTitle:@"确认" Click:^{
                [HELPDFfileCacheTool removeAllCourses:^{
                    [[RLMCourseManager manager] deleteAllObjects];
                    [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
                }];

            }];

        }
            break;
            
        case 1:{
            BTAdviceVC *adviceVC = [[BTAdviceVC alloc] init];
            adviceVC.title = @"意见反馈";
            adviceVC.limitCount = 200;
            [self.navigationController pushViewController:adviceVC animated:YES];
        }
            break;
        case 2: {
            CWQ_aboutUsVC *aboutUsVC = [CWQ_aboutUsVC new];
            aboutUsVC.title = @"关于我们";
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
            break;
        case 3: {
        }
            break;
            
        default:
            break;
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
